<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="couponModeId">
        <el-input
          v-model="queryParams.couponModeId"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="券名" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入券名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="couponType">
        <el-select v-model="queryParams.couponType" placeholder="请选择券类型" clearable size="small">
          <el-option label="限时券" value="1" />
      	  <el-option label="限量券" value="2" />
      	  <el-option label="特惠券" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布人" prop="createdName">
        <el-input
          v-model="queryParams.createdName"
          placeholder="请输入发布人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item label="组织号" prop="orgId">
	    <el-input
	      v-model="queryParams.orgId"
	      placeholder="请输入组织号"
	      clearable
	      size="small"
	      @keyup.enter.native="handleQuery"
	    />
	  </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:mode:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:mode:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="couponModeId" />
      <el-table-column label="券名" align="center" prop="couponName" />
      <el-table-column label="类型" align="center" prop="couponType" />
      <el-table-column label="组织号" align="center" prop="orgId" />
      <el-table-column label="使用门槛金额" align="center" prop="thresholdPrice" width="180"/>
      <el-table-column label="折扣比例" align="center" prop="discountRatio" />
      <el-table-column label="最大优惠价格" align="center" prop="maxCouponPrice" width="180"/>
      <el-table-column label="启用时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="停用时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="createdName" />
      <el-table-column label="发布时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
	  <el-table-column label="修改人" align="center" prop="modifierName" />
	  <el-table-column label="最后修改时间" align="center" prop="modifierTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifierTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="isEnable" />
	  <el-table-column
	    label="操作"
	    align="center"
	    width="160"
	    class-name="small-padding fixed-width"
	  >
	    <template slot-scope="scope">
	      <el-button
	        size="mini"
	        type="text"
	        icon="el-icon-edit"
	        @click="handleUpdate(scope.row)"
	        v-hasPermi="['system:user:edit']"
	      >修改</el-button>
	      <el-button
	        v-if="scope.row.userId !== 1"
	        size="mini"
	        type="text"
	        icon="el-icon-delete"
	        @click="handleDelete(scope.row)"
	        v-hasPermi="['system:user:remove']"
	      >删除</el-button>
	    </template>
	  </el-table-column>
      
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="券名" prop="couponName">
          <el-input v-model="form.couponName" placeholder="请输入券名" />
        </el-form-item>
        <el-form-item label="类型" prop="couponType">
          <el-select v-model="queryParams.couponType" placeholder="请选择券类型" clearable size="small">
            <el-option label="限时券" value="1" />
            <el-option label="限量券" value="2" />
            <el-option label="特惠券" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="组织号" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入组织号" />
        </el-form-item>
        <el-form-item label="使用门槛金额" prop="thresholdPrice">
          <el-input v-model="form.thresholdPrice" placeholder="请输入使用门槛金额" />
        </el-form-item>
        <el-form-item label="折扣比例" prop="discountRatio">
          <el-input v-model="form.discountRatio" placeholder="请输入折扣比例" />
        </el-form-item>
        <el-form-item label="优惠价格" prop="couponPrice">
          <el-input v-model="form.couponPrice" placeholder="请输入优惠价格" />
        </el-form-item>
        <el-form-item label="最大优惠价格" prop="maxCouponPrice">
          <el-input v-model="form.maxCouponPrice" placeholder="请输入最大优惠价格" />
        </el-form-item>
        <el-form-item label="启用时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择启用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停用时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择停用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="过期时间(秒)" prop="expire">
          <el-input v-model="form.expire" placeholder="请输入过期时间(秒)" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnable">
          <el-input v-model="form.isEnable" placeholder="请输入是否启用" />
        </el-form-item>
        <el-form-item label="发布人" prop="created">
          <el-input v-model="form.created" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable size="small"
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布人姓名" prop="createdName">
          <el-input v-model="form.createdName" placeholder="请输入发布人姓名" />
        </el-form-item>
        <el-form-item label="最后修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入最后修改人" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="modifierTime">
          <el-date-picker clearable size="small"
            v-model="form.modifierTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人姓名" prop="modifierName">
          <el-input v-model="form.modifierName" placeholder="请输入修改人姓名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMode, getMode, delMode, addMode, updateMode, exportMode } from "@/api/system/mode";

export default {
  name: "Mode",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券模板表格数据
      modeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponModeId: null,
        couponName: null,
        couponType: null,
        orgId: null,
        thresholdPrice: null,
        discountRatio: null,
        couponPrice: null,
        maxCouponPrice: null,
        startTime: null,
        endTime: null,
        expire: null,
        isEnable: null,
        created: null,
        createdTime: null,
        createdName: null,
        modifier: null,
        modifierTime: null,
        modifierName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询优惠券模板列表 */
    getList() {
      this.loading = true;
      listMode(this.queryParams).then(response => {
        this.modeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        couponModeId: null,
        couponName: null,
        couponType: null,
        orgId: null,
        thresholdPrice: null,
        discountRatio: null,
        couponPrice: null,
        maxCouponPrice: null,
        startTime: null,
        endTime: null,
        expire: null,
        isEnable: null,
        created: null,
        createdTime: null,
        createdName: null,
        modifier: null,
        modifierTime: null,
        modifierName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.couponModeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const couponModeId = row.couponModeId || this.ids
      getMode(couponModeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.couponModeId != null) {
            updateMode(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMode(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const couponModeIds = row.couponModeId || this.ids;
      this.$confirm('是否确认删除编号为"' + couponModeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMode(couponModeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优惠券模板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMode(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
